package com.test.clyq.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.test.clyq.entity.Option;
import com.test.clyq.entity.Users;
import com.test.clyq.service.OptionService;
import com.test.clyq.service.QuestionService;
import com.test.json.JsonUtil;
import com.test.util.FormatExceptionStack;
import com.test.util.ObjectUtils;
import com.test.util.SessionUtil;
import com.test.util.StringUtils;
import com.test.webapp.action.BaseAction;

/**
 * OptionAction. <br>
 * 评价选项管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:52:10
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: suncg@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class OptionAction extends BaseAction {

	/**
	 * 添加一个评价选项
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward addOption(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("添加一个评价选项表start");
		OptionService optionService = (OptionService) getBean("optionService");
		QuestionService questionService = (QuestionService) getBean("questionService");
		String questionId = getParameter(request, "questionId");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			if (!ObjectUtils.isNull(user) && StringUtils.isNumeric(questionId)) {
				Option option = new Option();
				option.setContent(getParameter(request, "content"));
				option.setCount(0l);
				option.setOptionQuestionId(Long.valueOf(questionId));
				// option.setQuestion(questionService.getOneQuestion(Long.valueOf(questionId)));
				optionService.addOption(option, user);
				this.log.debug("添加一个评价选项成功");
				this.log.info("添加一个评价选项end");
				print(response, true);
			}
		}
		catch (Exception e) {
			this.log.error("添加一个评价选项:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 删除评价选项
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward deleteOption(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("删除评价选项start");
		OptionService optionService = (OptionService) getBean("optionService");
		String optionId = getParameter(request, "optionId");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			if (!ObjectUtils.isNull(user) && StringUtils.isNumeric(optionId)) {
				optionService.deleteOption(Long.valueOf(optionId), user);
				print(response, true);
				this.log.debug("删除评价选项成功");
				this.log.info("删除评价选项end");
			}
		}
		catch (Exception e) {
			this.log.error("删除评价选项:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;

	}

	/**
	 * 获得一个评价选项
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getOption(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得一个评价选项表start");
		OptionService optionService = (OptionService) getBean("optionService");
		String questionId = getParameter(request, "questionId");
		try {
			if (StringUtils.isNumeric(questionId)) {
				Option option = optionService.getOneOption(Long.valueOf(questionId));
				if (!ObjectUtils.isNull(option)) {
					this.log.debug("获得一个评价选项成功");
					this.log.info("获得一个评价选项end");
					print(response, "{results:[{%s}]}", null, null, option.toJSON());
				} else {
					print(response, false);
				}
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("获得一个评价选项:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 获得评价选项列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getOptions(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得评价选项列表start");
		OptionService optionService = (OptionService) getBean("optionService");
		String questionId = getParameter(request, "questionId");
		try {
			if (StringUtils.isNumeric(questionId)) {
				List<Option> list = optionService.getOptions(Long.valueOf(questionId));
				String jsonStr = JsonUtil.getJsonStr(list, new String[] { "id", "content", "count" }, 100);
				this.log.debug("获得评价选项列表成功");
				this.log.info("获得评价选项列表end");
				print(response, jsonStr, null, null);
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("获得评价选项:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 修改一个评价选项
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward updatedownloadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("修改一个评价选项表start");
		OptionService optionService = (OptionService) getBean("optionService");
		Users user = SessionUtil.getUserFromSession(request);
		String optionId = getParameter(request, "optionId");
		try {
			if (!ObjectUtils.isNull(user) && StringUtils.isNumeric(optionId)) {
				Option option = optionService.getOneOption(Long.valueOf(optionId));
				option.setContent(getParameter(request, "content"));
				optionService.updateOption(option, user);
				print(response, true);
				this.log.debug("修改一个评价选项成功");
				this.log.info("修改一个评价选项end");
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("修改一个评价选项:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 修改一个评价选项表投票数
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward updatePostCount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("修改一个评价选项表投票数start");
		OptionService optionService = (OptionService) getBean("optionService");
		Users user = SessionUtil.getUserFromSession(request);
		String optionId = getParameter(request, "id");
		try {
			if (!ObjectUtils.isNull(user) && StringUtils.isNumeric(optionId)) {
				Option option = optionService.getOneOption(Long.valueOf(optionId));
				if (StringUtils.isNumeric(getParameter(request, "count"))) {
					option.setCount(Long.valueOf(getParameter(request, "count")));
				}
				optionService.updateOption(option, user);
				print(response, true);
				this.log.debug("修改一个评价选项成功");
				this.log.info("修改一个评价选项end");
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("修改一个评价选项:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}
}
